#include "stdafx.h"
#include "InstanceManagerFuncs.h"
#include "InstanceManager.h"

InstanceManager* g_pInstanceManager = NULL;

bool __stdcall InitManager()
{
	if (g_pInstanceManager == NULL) {
		g_pInstanceManager = new InstanceManager();
		return g_pInstanceManager->Init();
	}
	else
		return false;
}

void __stdcall ReleaseManager()
{
	if (g_pInstanceManager) {
		g_pInstanceManager->Release();
		delete g_pInstanceManager;
		g_pInstanceManager = NULL;
	}
}

bool __stdcall OpenExcel(const char* szExcelName)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->openExcel(QString::fromLocal8Bit(szExcelName));
}

bool __stdcall SaveExcel(const char* szPath /*= NULL*/)
{
	if (!g_pInstanceManager)
		return false;

	if (szPath == NULL)
		return g_pInstanceManager->saveExcel();
	else 
		return g_pInstanceManager->saveExcel(QString::fromLocal8Bit(szPath));
}

bool __stdcall CloseExcel()
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->closeExcel();
}

bool __stdcall GetSheetNames(unsigned char* pSheetNames, unsigned int* len)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QStringList sheetNameList;
	ret = g_pInstanceManager->getSheetNames(sheetNameList);
	if (ret) {
		QByteArray sheetNamesData = sheetNameList.join(QChar('\0')).toLocal8Bit();
		sheetNamesData.append('\0');
		if (pSheetNames == NULL) {
			*len = sheetNamesData.length();
		}
		else {
			memcpy(pSheetNames, sheetNamesData.data(), sheetNamesData.length());
			*len = sheetNamesData.length();
		}
	}

	return ret;
}

bool __stdcall CreateSheet(const char* szSheetName)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->createSheet(QString::fromLocal8Bit(szSheetName));
}

bool __stdcall DestroySheet(const char* szSheetName)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->destroySheet(QString::fromLocal8Bit(szSheetName));
}

bool __stdcall InsertInstance(const char* szSheetName, int index)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->insertInstance(QString::fromLocal8Bit(szSheetName), index);
}

bool __stdcall DeleteInstance(const char* szSheetName, int index)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->deleteInstance(QString::fromLocal8Bit(szSheetName), index);
}

bool __stdcall MoveUpInstance(const char* szSheetName, int index)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->moveUpInstance(QString::fromLocal8Bit(szSheetName), index);
}

bool __stdcall MoveDownInstance(const char* szSheetName, int index)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->moveDownInstance(QString::fromLocal8Bit(szSheetName), index);
}

int __stdcall GetInstanceCount(const char* szSheetName)
{
	if (!g_pInstanceManager)
		return -1;

	return g_pInstanceManager->getInstanceCount(QString::fromLocal8Bit(szSheetName));
}

bool __stdcall SetFunctionName(const char* szSheetName, int index, const char* szFunctionName)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setFunctionName(QString::fromLocal8Bit(szSheetName), index, QString::fromLocal8Bit(szFunctionName));
}

bool __stdcall GetFunctionName(const char* szSheetName, int index, char* szFunctionName)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString functionName;
	ret = g_pInstanceManager->getFunctionName(QString::fromLocal8Bit(szSheetName), index, functionName);
	if (ret)
		strcpy(szFunctionName, functionName.toLocal8Bit());

	return ret;
}

bool __stdcall SetFrontCondition(const char* szSheetName, int index, const char* szFrontCondition)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setFrontCondition(QString::fromLocal8Bit(szSheetName), index, QString::fromLocal8Bit(szFrontCondition));
}

bool __stdcall GetFrontCondition(const char* szSheetName, int index, char* szFrontCondition)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString frontCondition;
	ret = g_pInstanceManager->getFrontCondition(QString::fromLocal8Bit(szSheetName), index, frontCondition);
	if (ret)
		strcpy(szFrontCondition, frontCondition.toLocal8Bit());

	return ret;
}

bool __stdcall SetDescription(const char* szSheetName, int index, const char* szDescription)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setDescription(QString::fromLocal8Bit(szSheetName), index, QString::fromLocal8Bit(szDescription));
}

bool __stdcall GetDescription(const char* szSheetName, int index, char* szDescription)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString description;
	ret = g_pInstanceManager->getDescription(QString::fromLocal8Bit(szSheetName), index, description);
	if (ret)
		strcpy(szDescription, description.toLocal8Bit());

	return ret;
}

bool __stdcall SetRearCondition(const char* szSheetName, int index, const char* szRearCondition)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setRearCondition(QString::fromLocal8Bit(szSheetName), index, QString::fromLocal8Bit(szRearCondition));
}

bool __stdcall GetRearCondition(const char* szSheetName, int index, char* szRearCondition)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString rearCondition;
	ret = g_pInstanceManager->getRearCondition(QString::fromLocal8Bit(szSheetName), index, rearCondition);
	if (ret)
		strcpy(szRearCondition, rearCondition.toLocal8Bit());

	return ret;
}

bool __stdcall SetExpectedRetValue(const char* szSheetName, int index, const char* szExpectedRetValue)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setExpectedRetValue(QString::fromLocal8Bit(szSheetName), index, QString::fromLocal8Bit(szExpectedRetValue));
}

bool __stdcall GetExpectedRetValue(const char* szSheetName, int index, char* szExpectedRetValue)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString expectedRetValue;
	ret = g_pInstanceManager->getExpectedRetValue(QString::fromLocal8Bit(szSheetName), index, expectedRetValue);
	if (ret)
		strcpy(szExpectedRetValue, expectedRetValue.toLocal8Bit());

	return ret;
}

bool __stdcall SetParamInputValue(const char* szSheetName, int index, int paramIndex, const char* szInputValue)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setParamInputValue(QString::fromLocal8Bit(szSheetName), index, paramIndex, QString::fromLocal8Bit(szInputValue));
}

bool __stdcall GetParamInputValue(const char* szSheetName, int index, int paramIndex, char* szInputValue)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString inputValue;
	ret = g_pInstanceManager->getParamInputValue(QString::fromLocal8Bit(szSheetName), index, paramIndex, inputValue);
	if (ret)
		strcpy(szInputValue, inputValue.toLocal8Bit());

	return ret;
}

bool __stdcall SetParamExpectedValue(const char* szSheetName, int index, int paramIndex, const char* szExpectedValue)
{
	if (!g_pInstanceManager)
		return false;

	return g_pInstanceManager->setParamExpectedValue(QString::fromLocal8Bit(szSheetName), index, paramIndex, QString::fromLocal8Bit(szExpectedValue));
}

bool __stdcall GetParamExpectedValue(const char* szSheetName, int index, int paramIndex, char* szExpectedValue)
{
	if (!g_pInstanceManager)
		return false;

	bool ret;

	QString expectedValue;
	ret = g_pInstanceManager->getParamExpectedValue(QString::fromLocal8Bit(szSheetName), index, paramIndex, expectedValue);
	if (ret)
		strcpy(szExpectedValue, expectedValue.toLocal8Bit());

	return ret;
}
